/**
 */
define(function (require, exports, module) {
    'use strict';
    function getDataByPid(param){
    	var key = param.key;
    	var pId = param.pId;
    	var data = param.data;
    	var id = param.id;
    	var result = [] ;
        $.each(data,function(i,obj){
        	if(obj[key]==pId){
                result.push(data[i]);
                var temp = getDataByPid({key:key,pId:obj[id],data:data,id:id});           
                if(temp.length>0){
                	result =result.concat(temp);
                }    
            } 
        	
        });
        return result;
    }
    exports.getDataByPid=getDataByPid;
    exports.convert2Tree=function(ary,conf){
    	var options = {id:'id',pid:'pId',name:'name',childs:'children'};
    	$.extend(true,options,conf||{});
    	for(var i=0;i<ary.length;i++){
    		ary[i][options.childs] = [];
    		for(var j=0;j<ary.length;j++){
        		if(ary[i][options.id] == ary[j][options.pid]){
        			ary[i][options.childs].push(ary[j]);
        			ary[j].find = true;
        		}
        	}
    	}
    	for(var i=ary.length-1;i>=0;i--){
    		if(ary[i].find)
    			ary.splice(i,1);
    	}
    	
    	return ary;
    };

    exports.eachTree=function(obj,callback,conf){
    	var options = {id:'id',pid:'pId',name:'name',childs:'children'};
    	$.extend(true,options,conf||{});
    	var res = callback(obj);
    	if(res==false) return false;
    	else if(res instanceof Array){
    		return [obj].concat(res);
    	}else if(res){
    		return [res];
    	}
    	if(obj[options.childs]&&obj[options.childs].length>0){
    		for(var i=0;i<obj[options.childs].length;i++){
    			res = exports.eachTree(obj[options.childs][i],callback,options);
    			if(res==false) return false;
    			else if(res instanceof Array){
    				return [obj].concat(res);
    			}else if(res){
    				return [res];
    			}
    		}
    	}
    };
});